package com.blog.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.*;
import java.util.ArrayList;

/**
 * @Description:T0D0
 * @Author XieLiKui
 * @Date 2021/10/16
 **/
@WebListener
public class ApplicationContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        //用于保存敏感词汇
        ArrayList<String> sensitiveWords = new ArrayList<>();
        //读取敏感词
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("sensitive_word.txt");
        InputStreamReader reader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(reader);
        try {
            //读取，放入集合
            String str ;
            while ((str = bufferedReader.readLine())!=null){
                sensitiveWords.add(str);
            }
            context.setAttribute("sensitiveWords",sensitiveWords);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("关键词汇读取失败");
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
